<template>
    <div>
        <el-alert
            type="info"
            :closable="false"
            show-icon>
            <p>指定渠道优先显示, 若未选择渠道, 则该公告全服通用.</p>
        </el-alert>
        <el-divider></el-divider>
        <div class="filter-container">
            <el-form label-position="left" :inline="true">
                <el-form-item label="渠道">
                    <select-app-source v-model="search.source"></select-app-source>
                </el-form-item>
                <el-form-item label="状态">
                    <el-select v-model="search.status" clearable class="filter-item" placeholder="状态筛选" style="width: 120px;">
                        <el-option
                            v-for="(item, index) in options.status"
                            :key="index"
                            :label="item.label"
                            :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="">
                    <el-button class="filter-item" icon="el-icon-search" type="primary" :loading="loading" @click="pageNo = 1;handleSearch()">刷新</el-button>
                    <el-button class="filter-item" icon="el-icon-plus" type="primary" @click="create" plain>添加</el-button>
                </el-form-item>
            </el-form>
        </div>

        <vxe-table
            :loading="loading"
            ref="xTable"
            stripe
            border
            :data="tableData"
            tooltip-effect="dark"
            style="width: 100%"
            class="text-center"
            @checkbox-all="handleSelectionChange"
            @checkbox-change="handleSelectionChange">
            <vxe-table-column type="checkbox" width="40"/>
            <vxe-table-column title="ID" field="id" width="50"/>
            <vxe-table-column title="渠道" field="source" formatter="formatAppSourceName" width="150" show-overflow/>
            <!--            <vxe-table-column title="开始时间" field="start_at" width="140"/>-->
            <!--            <vxe-table-column title="结束时间" field="end_at" width="140"/>-->
            <vxe-table-column title="状态" field="status" width="100">
                <template slot-scope="scope">
                    <el-switch size="mini" :disabled="loading" v-model="scope.row.status" :active-value="1" :inactive-value="0" @change="update($event, 'status', scope.row.id)" active-color="#13ce66"/>
                </template>
            </vxe-table-column>
            <vxe-table-column title="内容" field="content" min-width="150" show-tooltip-when-overflow/>
            <vxe-table-column title="更新时间" field="updated_at" width="140"/>
            <vxe-table-column title="操 作" width="150px">
                <template slot-scope="scope">
                    <el-button
                        type="primary"
                        @click="edit(scope.row)">
                        编辑
                    </el-button>
                    <destroy-confirm
                        route="announce/Destroy"
                        :primary="scope.row.id"
                        @success="handleSearch">
                    </destroy-confirm>
                </template>
            </vxe-table-column>
        </vxe-table>

        <div class="mt-10 pull-left">
            <destroy-multiple
                route="announce/DestroyMultiple"
                :selected="selected"
                @success="handleSearch">
                <el-button type="danger" plain>删除选中</el-button>
            </destroy-multiple>
        </div>

        <el-pagination
            class="pull-right"
            background
            :current-page.sync="pageNo"
            :page-sizes="pageOptions.pageSizesList"
            :page-size="pageSize"
            :layout="pageOptions.layout"
            :total="totalDataNumber"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange">
        </el-pagination>

        <el-dialog
            :title="form.data && form.data.id ? '编辑' : '添加'"
            :visible.sync="form.show"
            width="30%"
            :append-to-body="true"
            center>
            <dialog-form
                :data="form.data"
                v-on:submit="onFormSubmit"
                v-on:cancel="form.show = false">
            </dialog-form>
        </el-dialog>

    </div>
</template>

<script>
import dialogForm from './form'
export default {
    components: {
        dialogForm,
    },
    data() {
        return {
            search: {
                source: '',
                status: '',
            },
            loading: false,
            selected: [],
            tableData: [],
            pageNo: 1,
            pageSize: 20,
            totalDataNumber: 0,
            pageOptions: this.$store.state.backend.pageOptions,
            options: this.$store.state.announce.options,
            form: {
                data: {},
                show: false,
            },
        }
    },
    computed: {

    },
    // watch: {
    //     '$route' (val) {
    //         if (val.name === 'announce.index') {
    //             this.initData();
    //         } else {
    //             this.stopRefreshTimeout();
    //         }
    //     }
    // },
    methods: {
        handleSizeChange(val) {
            this.pageNo = 1;
            this.pageSize = parseInt(`${val}`);
            this.handleSearch();
        },
        handleCurrentChange(val) {
            this.pageNo = parseInt(`${val}`);
            this.handleSearch();
        },
        handleSelectionChange(val) {
            this.selected = val.records.map(v => v.id);
        },
        handleSearch() {
            this.loading = true;
            let filter = Object.assign({}, this.search, {
                page: this.pageNo,
                pageSize: this.pageSize,
            });

            this.$store.dispatch('announce/Lists', filter).then(res => {
                this.tableData = res.data.data;
                this.$refs.xTable.reloadData(this.tableData);
                this.totalDataNumber = res.data.meta.total;
            }).finally(() => {
                this.loading = false;
            });
        },
        initData() {
            this.handleSearch();
        },
        create() {
            this.form.data = {};
            this.form.show = true;
        },
        edit(row) {
            this.form.data = row;
            this.form.show = true;
        },
        onFormSubmit() {
            this.form.show = false;
            this.handleSearch();
        },
        update(val, field, id) {
            let obj = {
                id: id,
            };
            obj[field] = val;
            this.$confirm('是否确认?', '提示', {
                confirmButtonText: '是',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.loading = true;
                this.$store.dispatch('announce/Update', obj).then(res => {
                    this.$message.success(res.data.message);
                }).finally(() => {
                    this.loading = false;
                })
            }).catch(() => {
                this.handleSearch();
            });
        },
    },
    created() {
        this.initData();
    },
}
</script>

<style lang="scss" scoped>
#app .el-form-item {
    margin-bottom: 0;
}
</style>


